/**
 * Copyright 2010 Terrestrial Ecosystem Research Network, licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
 * agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package au.org.tern.ecoinformatics.oai.provider.web;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

import ORG.oclc.oai.server.OAIHandler;
import ORG.oclc.oai.server.catalog.AbstractCatalog;
import au.org.tern.ecoinformatics.oai.provider.util.StaticApplicationContextHelper;

/**
 * Attribution: This code extends the OAICat servlet from Online Computer Library Center (OLCL).
 * 
 * It is extended to make use of the Spring Application Context for looking up custom OAICatalog classes, 
 * rather than effectively running two parallel bean containers.
 *  
 * @author vhobbs
 *
 */
public class OaiHandler extends OAIHandler {

	private static final long serialVersionUID = 8158430668587960345L;
	private static final String VERSION = "1.5.57";
	
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public HashMap getAttributes(Properties properties) {
    	try {
        HashMap attributes = new HashMap();
        Enumeration attrNames = getServletContext().getAttributeNames();
        while (attrNames.hasMoreElements()) {
            String attrName = (String)attrNames.nextElement();
            attributes.put(attrName, getServletContext().getAttribute(attrName));
        }
        attributes.put("OAIHandler.properties", properties);
        String missingVerbClassName = properties.getProperty("OAIHandler.missingVerbClassName", "ORG.oclc.oai.server.verb.BadVerb");
        Class missingVerbClass = Class.forName(missingVerbClassName);
        attributes.put("OAIHandler.missingVerbClass", missingVerbClass);
        if (!"true".equals(properties.getProperty("OAIHandler.serviceUnavailable"))) {
            attributes.put("OAIHandler.version", VERSION);
            AbstractCatalog abstractCatalog = (AbstractCatalog)StaticApplicationContextHelper.getApplicationContext().getBean("oaiCatalog");
            attributes.put("OAIHandler.catalog", abstractCatalog);
        }
        String appBase = properties.getProperty("OAIHandler.appBase");
        if (appBase == null) 
        		appBase = "webapps";

        return attributes;
        }
    	catch (Throwable t) {
    		throw new RuntimeException(t);
    	}
    }
	
}
